package com.css.fxfzypg.modules.group.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.modules.group.service.GroupService;
import com.css.fxfzypg.modules.group.vo.GroupMemberVo;
import com.css.fxfzypg.modules.group.vo.GroupVo;
import com.css.fxfzypg.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/group")
@Slf4j
public class GroupController {

    @Autowired
    GroupService groupService;

    /**
     * 新建小组
     * @param
     * @return
     */
    @PostMapping("/save")
    public RestResponse saveGroup(@RequestBody GroupMemberVo groupMemberVo){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try {
            groupService.saveGroup(groupMemberVo);
            jsonObject.put("message","保存成功");
            restResponse=RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errormessage="保存失败";
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /***
     * 小组列表分页查询
     * @return
     */
    @GetMapping("/page")
    public RestResponse getPage(HttpServletRequest request, GroupVo GroupVo){
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize=Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> page=groupService.getPage(request,GroupVo,curPage,pageSize);
            json.put("message", "查询成功!");
            response = RestResponse.succeed(page);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 批量删除
     * @param
     * @return
     */
    @PostMapping("/delete")
    public RestResponse deletGroup(String ids){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try {
            String[] arr= ids.split(",");
            List<String> list = Arrays.asList(arr);
            groupService.deletGroup(list);
            jsonObject.put("message","删除成功");
            restResponse= RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errormessage="删除失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /***
     * 小组查看
     * @param groupId
     * @return
     */
    @GetMapping("/findById")
    public RestResponse findById(String groupId){
        RestResponse restResponse=null;
        try {
            GroupMemberVo GroupMemberVo=groupService.findById(groupId);
            restResponse= RestResponse.succeed(GroupMemberVo);
        }catch (Exception e){
            String errormessage="查看失败";
            log.error(errormessage,e);
            restResponse= RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 小组修改
     * @param
     * @return
     */
    @PostMapping("/update")
    public RestResponse updateGroup(@RequestBody GroupMemberVo groupMemberVo){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try {
            groupService.updateGroup(groupMemberVo);
            jsonObject.put("message","修改成功");
            restResponse=RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errormessage="修改失败";
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }
}